<?php
namespace Lib\Finance;
/**
 * Description of Finance
 * 财务提醒数据格式。共用xia_office_log表存储，但针对财务提醒消息制定特殊格式。如下：
 * 采用json格式。字段code：消息类型，10000为提醒雇员实际发薪，10001为提醒雇主，雇员申请发薪
 *                  text：正文
 * sample：{code:10000,text:" 歪乐团队向您实际发薪1000元（约定薪酬为2000元），请到“进行中”操作"}
 *  
 * @author chacha
 * @date 2016-6-3
 */
class Finance extends \BaseAction{
    
    protected $roleflag = 0;


    protected $tabIncomelog = NULL;//收入表
    protected $tabPayoutlog = NULL;//支出表
    protected $tabWithdraw = NULL;//提现表
    protected $tabInstalstage = NULL;//分期托管表
    protected $tabWallet = NULL;//钱包表
    protected $tabChargelog = NULL;//充值表

    protected $totalIncome = 0;
    protected $totalPayout = 0;
    protected $totalTuoguan = 0;
    protected $rest = 0;//余额
    protected $incomeArr = NULL;
    protected $payoutArr = NULL;
    protected $withdrawArr = NULL;
    protected $instalstageArr = NULL;
    protected $chargeArr = NULL;

    public function __construct($uid, $userinfo, $roleflag) {
        parent::__construct();
        
        $this->uid = $uid;
        $this->userinfo = $userinfo;
        $this->roleflag = $roleflag;
        
        $this->tabIncomelog = M('income_log');
        $this->tabPayoutlog = M('payout_log');
        $this->tabWithdraw = M('withdraw_log');
        $this->tabInstalstage = M('instal_stage');
        $this->tabWallet = M('wallet');
        $this->tabChargelog = M('charge_log');//xia_charge_log
    }
    
    public function process() {
        if ($this->roleflag==1) {
            $this->personal();
        }
        else if ($this->roleflag==2) {
            $this->cop();
        }
    }
    
    private function cop() {
        $this->basedata();
        
        require "Cop.class.php";
        $cop = new Cop($this->uid);
        $this->totalTuoguan = $cop->getTuoguan();
        $tipArr = $cop->getTips();
        $doingjob = $cop->getDoingjob();
        $this->assign('tipArr', $tipArr);
        $this->assign('doingjobArr', $doingjob);
        $team = $cop->getTeam();
        $this->assign('tname', $team['name']);
        
        $this->baseRender("cop");
    }
    
    private function personal() {
        $this->basedata();
        
        require "Personal.class.php";
        $personal = new Personal($this->uid);
        $this->totalTuoguan = $personal->getTuoguan();
        $tipArr = $personal->getTips();
        $doingjob = $personal->getDoingjob();
        
        $this->assign('tipArr', $tipArr);
        $this->assign('doingjobArr', $doingjob);
        
        $this->baseRender("personal");
    }
    
    //基础数据
    protected function basedata() {
        //收入
        $totalIncome = $this->tabIncomelog->where("uid=".$this->uid." and status=1")->sum("income");
        $this->totalIncome = is_null($totalIncome)?0:$totalIncome;
        $this->incomeArr = $this->income();
        //支出
        $totalPayout = $this->tabPayoutlog->where("uid=".$this->uid." and status=1")->sum("payout");
        $this->totalPayout = is_null($totalPayout)?0:$totalPayout;
        $this->payoutArr = $this->payout();
        
        //余额
        $wallet = $this->tabWallet->where("uid=".$this->uid)->find();
        if ($wallet) {
            $this->rest = $wallet['cash'];
        }
        
        //提现记录
        $this->withdrawArr = $this->withdraw();
        //充值记录
        $this->chargeArr = $this->charge();
    }
    
    //统计收入
    protected function income() {
        $allIncome = $this->tabIncomelog->where("uid=".$this->uid." and status=1")->select();
        return $allIncome;
    }
    
    //统计支出
    protected function payout() {
        $allPayout = $this->tabPayoutlog->where("uid=".$this->uid." and status=1")->select();
        return $allPayout;
    }
    
    //统计提现
    protected function withdraw() {
        $allWithdraw = $this->tabWithdraw->where("uid=".$this->uid)->select();
        return $allWithdraw;
    }
    
    //统计充值
    protected function charge() {
        $allCharge = $this->tabChargelog->where("uid=".$this->uid)->select();
        return $allCharge;
    }
    
    //基础数据渲染
    protected function baseRender($tpl) {
        $this->assign("totalIncome", $this->totalIncome);
        $this->assign("totalPayout", $this->totalPayout);
        $this->assign('incomeArr', $this->incomeArr);
        $this->assign('payoutArr', $this->payoutArr);
        $this->assign('withdrawArr', $this->withdrawArr);
        $this->assign("chargeArr", $this->chargeArr);
        $this->assign("totalTuoguan", $this->totalTuoguan);
        $this->assign("rest", $this->rest);
        $this->display($tpl);
    }
    
    //取消提现申请
    public function cancel_withdraw($wid) {
        $ret = M('withdraw_log')->where("id=$wid and uid=".$this->uid)->save(array('process'=> \App\Common\Constvars::WITHDRAW_CANCEL));
        if ($ret===false) {
            return false;
        }
        else {
            return true;
        }
    }
    
    //催促托管/付款
    public function urgepay($jobid, $stageid, $urgetype) {
        //取团队信息
        $team = M('team')->where("id=".$this->userinfo['teamid'])->find();
        $rs = M('urgepay')->where("jobid=$jobid and stageid=$stageid")->find();
        //未催促过
        if (!$rs) {
            $data = array(
                'jobid'=>$jobid,
                'stageid'=>$stageid,
                'urgetype'=>$urgetype,
                'boss'=>$team['uid'],
            );
            $ret = M('urgepay')->add($data);
            if ($ret) {
                return true;
            }
            else {
                return false;
            }
        }
        //已催促过
        else {
            if ($rs['urgetype']==$urgetype) {
                return true;
            }
            else {
                $ret = M('urgepay')->where("jobid=$jobid and stageid=$stageid")->save(array('urgetype'=>$urgetype));
                if ($ret===false) {
                    return false;
                }
                else {
                    return true;
                }
            }
        }
    }
    
    //设置催促信息已读(删除记录)
    public function delurge($urgeid) {
        $rs = M('urgepay')->where("id=$urgeid")->find();
        if ($rs['boss']==$this->uid) {
            $ret = M('urgepay')->where("id=$urgeid")->delete();
            if ($ret===false) {
                ajaxf();
            }
            else {
                ajaxs();
            }
        }
        else {
            ajaxf("无权限删除该条催促消息");
        }
    }
}
